package com.gree.scmservice.controller.saas.tenant;

import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.TablePageData;
import com.gree.common.utils.MinIoUtil;
import com.gree.common.utils.ReturnCode;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.entity.saas.SaasSysOrder;
import com.gree.scmservice.service.saas.SaasOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.nutz.dao.DaoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 包路径: com.gree.scmservice.controller.saas
 * 项目名: scm-api
 * 创建时间: 2021-07-13 9:58
 * 描述: TODO
 **/
@Api(tags = "系统订单信息(租户端)")
@RestController
@RequestMapping("/saas/tenant/order")
public class SaasOrderController {

    @Autowired
    private SaasOrderService saasOrderService;

    /**
     * 查询系统订单信息列表(租户使用)
     */
    @GetMapping(value = "/listonuser")
    @ApiOperation(value = "查询系统订单信息列表(租户使用)")
    @ApiImplicitParam(name = "saasSysOrder", value = "系统订单信息", dataType = "SaasSysOrder")
    public ReturnVo<TablePageData<SaasSysOrder>> listOnUser(SaasSysOrder saasSysOrder) {
        TablePageData<SaasSysOrder> saasSysOrderTablePageData = saasOrderService.selectOrderPageOnUser(saasSysOrder);
        return ReturnVo.success(saasSysOrderTablePageData);
    }

    /**
     * 获取系统订单信息详细信息(租户使用)
     */
    @GetMapping(value = "/{orderNo}")
    @ApiOperation(value = "获取系统订单信息详细信息(租户使用)")
    @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "String")
    public ReturnVo getInfo(@PathVariable("orderNo") String orderNo)
    {
        return ReturnVo.success(saasOrderService.getOrderNo(orderNo));
    }

    /**
     * 获取系统订单信息详细信息(流水号)
     */
    @GetMapping(value = "/out_trade_no/{outTradeNo}")
    @ApiOperation(value = "获取系统订单信息详细信息(流水号)")
    @ApiImplicitParam(name = "outTradeNo", value = "订单号", dataType = "String")
    public ReturnVo getInfoByTradeNoOrder(@PathVariable("outTradeNo") String outTradeNo)
    {
        return ReturnVo.success(saasOrderService.getOutTradeNoOrder(outTradeNo));
    }

    /**
     * 检查商品状态
     */
    @GetMapping("/check")
    @ApiOperation(value = "检查商品状态")
    @ApiImplicitParam(name = "groupId", value = "商品号", dataType = "String")
    public ReturnVo getCheckOutTradeNo(@RequestParam("group_id") String groupId) {
        return ReturnVo.toAjax(saasOrderService.getCheckOutTradeNo(groupId));
    }

    /**
     * 提交订单
     */
    @PostMapping(value = "/submit")
    @ApiOperation(value = "提交订单")
    @ApiImplicitParam(name = "saasSysOrder", value = "系统订单信息", dataType = "SaasSysOrder")
    public ReturnVo insert(@RequestBody SaasSysOrder saasSysOrder){
        return ReturnVo.toAjax(saasOrderService.saveOrder(saasSysOrder));
    }

    /**
     * 修改系统订单信息
     */
    @PutMapping
    @ApiOperation(value = "修改系统订单信息")
    @ApiImplicitParam(name = "saasSysOrder", value = "系统订单信息", dataType = "SaasSysOrder")
    public ReturnVo edit(@RequestBody SaasSysOrder saasSysOrder)
    {
        return ReturnVo.toAjax(saasOrderService.updateOrder(saasSysOrder));
    }

    /**
     * 软删除
     */
    @DeleteMapping("/del/{orderNos}")
    @ApiOperation(value = "软删除系统订单信息")
    @ApiImplicitParam(name = "orderNos", value = "订单号", dataType = "String[]")
    public ReturnVo removeUpdate(@PathVariable String[] orderNos)
    {
        return ReturnVo.toAjax(saasOrderService.updateDelOrderByIds(orderNos));
    }

    /**
     * 线下支付（上传文件，说明）
     */
    @PutMapping(value = "/paytype")
    @ApiOperation(value = "线下付款（上传文件，说明）")
    @ApiImplicitParam(name = "saasSysOrder", value = "系统订单信息", dataType = "SaasSysOrder")
    public ReturnVo updatePayTypeOrder(@RequestBody SaasSysOrder saasSysOrder){
        return ReturnVo.toAjax(saasOrderService.updatePayTypeOrder(saasSysOrder));
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传支付信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "MultipartFile"),
            @ApiImplicitParam(name = "orderNo", value = "支付信息ID", dataType = "String")
    })
    public ReturnVo upload(MultipartFile file, String orderNo) {
        return saasOrderService.uploadPayCert(file, orderNo);
    }

    /**
     * 下载文件
     */
    @GetMapping("/download")
    @ApiOperation(value = "下载支付信息")
    @ApiImplicitParam(name = "path", value = "支付信息图片", dataType = "String")
    public void download(@RequestParam String path, HttpServletResponse response) {
        saasOrderService.downloadPayCert(path, response);
    }


}
